// const { default: axios } = require("axios");

class Member {
	constructor() {
		axios.defaults.headers.common["SAT"] = "1234567";
	}
	Axios(callback) {
		axios
			.post("https://ty.php800.cn/api/member/search.php", "", {
				headers: {
					TOKEN: localStorage.getItem("token"),
					SAT: "1234567",
				},
			})
			.then((result) => {
				let { verify, msg, member } = result.data;
				if (verify) {
					if (typeof callback === "function") {
						callback(member, this);
					}
				} else {
					console.log(msg);
				}
			})
			.catch((err) => console.log(err));
	}
	getLocalTime(nS) {
		return new Date(parseInt(nS)).toLocaleString().replace(/:\d{1,2}$/, " ");
	}
	getdate(date) {
		var now = new Date(date),
			y = now.getFullYear(),
			m = now.getMonth() + 1,
			d = now.getDate();
		return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d);
	}
	//获取会员列表
	setmemberlist(fa, el, item) {
		let { id, nickname, gender, roleid, authority, registrationtime, phone, email } = item;
		let authority_name = "";
		if (authority === "0") {
			authority_name = "超级管理员";
		} else if (authority === "1") {
			authority_name = "普通管理员";
		} else if (authority === "9") {
			authority_name = "注册会员";
		} else {
			authority_name = "注册会员";
		}
		let dl = document.createElement(el);
		let a = parseInt(registrationtime);
		a = this.getdate(a);

		dl.innerHTML = `
						<dd><p>${id}</p></dd>
						<dd><p>${nickname}</p></dd>
						<dd><p>${gender === "1" ? "男" : "女"}</p></dd>
						<dd><p class='role_id'>${roleid}</p></dd>
						<dd><p>${authority_name}</p></dd>
						<dd><p>${a}</p></dd>
						<dd><p>${phone}</p></dd>
						<dd><p>${email}</p></dd>
						<dd>
						<a href="change_password.html?id=${id}" class="editpassword ">更改密码</a>
						<a href="Data_modification.html?id=${id}" class="editdata">修改资料</a>
						</dd>
						`;
		let QAQ = document.querySelector(fa);
		QAQ.appendChild(dl);
	}
}

let member = new Member();
member.Axios(function (res, _this) {
	let page = document.querySelector(".page");
	let QAQ = document.querySelector("#QAQ");
	let arr = [],
		num = 0,
		m = 0;
	for (let i = 0; i < res.length / 10; i++) {
		arr[i] = [];
	}
	res.forEach((item) => {
		if (num < 10) {
			num++;
		} else {
			m++;
			num = 1;
		}
		arr[m].push(item);
	});

	//会员列表
	arr[parseInt(0)].forEach(function (item) {
		_this.__proto__.setmemberlist("#QAQ", "dl", item);
	});

	for (let i = 0; i < arr.length; i++) {
		let span = document.createElement("span");
		span.innerHTML = i + 1;
		span.addEventListener("click", function () {
			QAQ.innerHTML = "";
			arr[parseInt(this.innerHTML - 1)].forEach((item) => {
				_this.__proto__.setmemberlist("#QAQ", "dl", item);
			});
		});

		page.appendChild(span);
		let sp = page.querySelectorAll("span");
		sp[0].classList.add("che");
		for (let i = 0; i < sp.length; i++) {
			sp[i].addEventListener("click", () => {
				for (let j = 0; j < sp.length; j++) {
					sp[j].classList.remove("che");
				}
				sp[i].classList.add("che");
			});
		}
	}
});
